using Microsoft.Xna.Framework;

namespace GorillaEngine.Core.Extensions {
    public static class Vector2Extensions
    {
        public static Vector2 Substract(this Vector2 me, Vector2 toSubstract)
        {
            me.X = me.X - toSubstract.X;
            me.Y = me.Y - toSubstract.Y;
            return me;
        }

        public static Vector2 Add(this Vector2 me, Vector2 toAdd)
        {
            me.X = me.X + toAdd.X;
            me.Y = me.Y + toAdd.Y;
            return me;
        }

        public static Vector2 Reduce(this Vector2 me, Vector2 toReduce) {
            me.X = ReduceTillZero(me.X, toReduce.X, me.X < 0);
            me.Y = ReduceTillZero(me.Y, toReduce.Y, me.Y < 0);
            return me;
        }

        private static float ReduceTillZero(float source, float toReduce, bool isNegative) {
            return isNegative 
                       ? Add(source, toReduce) 
                       : Substract(source, toReduce);
        }

        private static float Add(float source, float toReduce) {
            
            source = source + toReduce;
            if (source > 0) {
                source = 0;
            }
            return source;
        }
        private static float Substract(float source, float toReduce) {
            
            source = source - toReduce;
            if (source < 0) {
                source = 0;
            }
            return source;
        }

        public static Vector2 GetVectorTo(this Vector2 me, Vector2 theOther) {
            var highestX = MathHelper.Max(me.X, theOther.X);
            var lowestX = MathHelper.Min(me.X, theOther.X);
            var highestY = MathHelper.Max(me.Y, theOther.Y);
            var lowestY = MathHelper.Min(me.Y, theOther.Y);
            
            return new Vector2(highestX - lowestX, highestY - lowestY);
        }
    }
}